'''------------------------------------------------------------------------------------------------
 $ @Author: Yangshangqi
 $ @Date: 2021-12-20 18:46:53
 $ @LastEditTime: 2021-12-22 20:54:31
 $ @LastEditors: Please set LastEditors
 $ @Description: bbox格式转换函数包
 $ @FilePath: \YSQTrackerEvaluationToolkit\TOOLS\bboxformat.py
--------------------------------------------------------------------------------------------------'''
import numpy as np

'''-------------------------------------------------------------------------------------------
 * @description:#* bbox格式转换函数 xywh 转 x1y1x2y2
 * @param {*} rect
 * @return {*}
---------------------------------------------------------------------------------------------'''
def xywh2xyxy(rect):
    rect = np.array(rect, dtype=np.float32)
    return np.concatenate([
        rect[..., [0]], rect[..., [1]], rect[..., [2]] + rect[..., [0]] - 1,
        rect[..., [3]] + rect[..., [1]] - 1
    ],axis=-1)

'''-------------------------------------------------------------------------------------------
 * @description:#* bbox格式转换函数 x1y1x2y2 转 xy(中心)wh
 * @param {*} bbox
 * @return {*}
---------------------------------------------------------------------------------------------'''
def xyxy2cxywh(bbox):
    bbox = np.array(bbox, dtype=np.float32)
    return np.concatenate([(bbox[..., [0]] + bbox[..., [2]]) / 2,
                        (bbox[..., [1]] + bbox[..., [3]]) / 2,
                        bbox[..., [2]] - bbox[..., [0]] + 1,
                        bbox[..., [3]] - bbox[..., [1]] + 1],
                        axis=-1)

'''-------------------------------------------------------------------------------------------
 * @description:#* bbox格式转换函数 x1y1x2y2 转 xywh
 * @param {*} bbox
 * @return {*}
---------------------------------------------------------------------------------------------'''
def xyxy2xywh(bbox):
    bbox = np.array(bbox, dtype=np.float32)
    return np.concatenate([
        bbox[..., [0]], bbox[..., [1]], bbox[..., [2]] - bbox[..., [0]] + 1,
        bbox[..., [3]] - bbox[..., [1]] + 1
    ],axis=-1)

'''-------------------------------------------------------------------------------------------
 * @description: #* bbox格式转换函数->xy(中心)wh  转  x1y1x2y2
 * @param {*} box
 * @return {*}
---------------------------------------------------------------------------------------------'''
def cxywh2xyxy(box):
    box = np.array(box, dtype=np.float32)
    return np.concatenate([
        box[..., [0]] - (box[..., [2]] - 1) / 2, box[..., [1]] -
        (box[..., [3]] - 1) / 2, box[..., [0]] +
        (box[..., [2]] - 1) / 2, box[..., [1]] + (box[..., [3]] - 1) / 2
    ],axis=-1)

'''-------------------------------------------------------------------------------------------
 * @description: #* bbox格式转换函数->xy(左上)wh  转  xy(中心)wh
 * @param {*} rect
 * @return {*}
---------------------------------------------------------------------------------------------'''
def xywh2cxywh(rect):
    rect = np.array(rect, dtype=np.float32)
    return np.concatenate([
        rect[..., [0]] + (rect[..., [2]] - 1) / 2, rect[..., [1]] +
        (rect[..., [3]] - 1) / 2, rect[..., [2]], rect[..., [3]]
    ],axis=-1)

'''-------------------------------------------------------------------------------------------
 * @description: #* bbox格式转换函数->xy(中心)wh 转  xy(左上)wh 
 * @param {*} box
 * @return {*}
---------------------------------------------------------------------------------------------'''
def cxywh2xywh(box):
    box = np.array(box, dtype=np.float32)
    return np.concatenate([
        box[..., [0]] - (box[..., [2]] - 1) / 2, box[..., [1]] -
        (box[..., [3]] - 1) / 2, box[..., [2]], box[..., [3]]
    ],
    axis=-1)


